<template>
  <div class="main">
    <div class="bg">
      <img src="@/assets/image.jpg" alt="" />
    </div>
    <div class="nav">
      <div class="logo">
        <h1>
          <router-link to="/">BFBbs</router-link>
        </h1>
      </div>
      <div class="options">
        <router-link to="/layout" class="router-link-active">
          发现
        </router-link>
        <router-link to="/layout/book" class="router-link-active">
          书籍
        </router-link>
        <router-link to="/layout/film" class="router-link-active">
          影视
        </router-link>
        <div class="info">
          <el-button v-if="!isShow" class="btnDiv" @click="goToLogin"
            >登陆/注册</el-button
          >
          <div v-else class="info-user">
            <span class="nickname">{{ info.nickname }}</span>
            <el-dropdown style="width: 30%">
              <span class="el-dropdown-link">
                <el-avatar :src="info.avatar || defaultAvatar"></el-avatar>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <span @click="$router.push('/layout/user')">个人信息</span>
                </el-dropdown-item>
                <el-dropdown-item divided>
                  <span @click="exit">退出登录</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <h2>🎉欢迎来到我们的书籍影视交流平台！🎉</h2>
      <p>
        这里，我们共同探索文学的深邃海洋与影视的绚烂星空。让我们在文字与影像的交织中，启迪智慧，触动心灵。无论您是寻找思想的共鸣，还是追寻艺术的灵感，这里都是您理想的栖息地。加入我们，开启一段精神成长与文化探索的旅程吧！
      </p>
      <el-button
        type="primary"
        class="btnEnter"
        @click="$router.push('/layout')"
        >立即加入</el-button
      >
    </div>
    <div class="filing">
      <div>武汉市洪山区关山街道光谷大道50号</div>
      <div>
        <a href="https://beian.miit.gov.cn/" target="_blank"
          >鄂ICP备2024084732号-1</a
        >
      </div>
    </div>
  </div>
</template>

<script>
import { isValidToken } from "@/utils/checkTokenValid";
import defaultAvatar from "@/assets/defaultAvatar.png";
import userInfo from "@/utils/userInfo";
import { logout } from "@/api/user";
export default {
  name: "MainView",
  data() {
    return {
      isShow: "",
      defaultAvatar,
      info: {},
    };
  },
  async created() {
    await isValidToken();
    this.isLogin();
    this.getUserInfo();
  },
  methods: {
    goToLogin() {
      this.$router.push("/login");
    },
    // 获取用户信息
    getUserInfo() {
      this.info = userInfo.getUser();
    },
    // 判断是否登陆
    isLogin() {
      const token = localStorage.getItem("token") ? true : false;
      this.isShow = token;
    },
    // 退出登录
    async exit() {
      await logout();
      localStorage.removeItem("token");
      localStorage.removeItem("userInfo");
      this.isLogin();
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  height: 690px;
  background-color: #f4f4f9;
  position: relative;
  .bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 70%;
    img {
      width: 100%;
      height: 100%;
    }
  }
  .nav {
    position: absolute;
    width: 100%;
    height: 60px;
    padding: 0 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .logo {
      .router-link-active {
        text-decoration: none;
        color: #fff;
      }
    }
    .options {
      width: 30%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .router-link-active {
        color: #fff;
        text-decoration: none;
        transition: background 0.3s, transform 0.1s;
        &:hover {
          transform: scale(1.08);
        }
      }
      .info {
        width: 26%;
        .info-user {
          display: flex;
          align-items: center;
          justify-content: space-between;
          .nickname {
            display: inline-block;
            width: 64%;
            font-size: 16px;
            color: #fff;
            text-align: right;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
      }
    }
  }
  .footer {
    position: absolute;
    left: 0;
    top: 72%;
    width: 100%;
    height: 210px;
    text-align: center;
    background-color: #fff;
    padding: 20px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    h2 {
      font-size: 2.5em;
      color: #333;
      margin-bottom: 14px;
    }
    p {
      font-size: 1.2em;
      color: #666;
      line-height: 1.6;
    }
    .btnEnter {
      margin-top: 14px;
    }
  }
  .filing {
    position: absolute;
    left: 0;
    top: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 60px;
    margin-top: 19px;
    width: 100%;
    height: 70px;
    background-color: #202b3d;
    color: #fff;
    a {
      color: #fff;
      text-decoration: none;
    }
  }
}
/*鼠标点击后移开，恢复本身样式*/
.btnDiv,
.btnDiv:focus:not(.btnDiv:hover) {
  margin-right: 12px;
  border-radius: 2px;
  box-shadow: 0 2px 4px 0 #f4f4f4;
  color: #2794f8;
  background: white;
}
/*鼠标按下，没有抬起*/
.btnDiv:active {
  border: 1px solid #fff;
  color: #2794f8;
}
// 纠正el-avatar的图片显示问题
/deep/.el-avatar > img {
  width: 100%;
}
</style>